package com.wswy.wzcx.ui.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.support.annotation.DrawableRes
import android.support.v7.app.AppCompatDialog
import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager
import com.wswy.wzcx.R
import com.wswy.wzcx.module.base.paddingZero
import kotlinx.android.synthetic.main.dialog_choose_date_img_layout.*

class ChooseDateDialog (context: Context,
                        @DrawableRes private val imgRes:Int,
                        private val title:String,


                        /**
                         * yyyy-MM-dd
                         */
                        private val defDate:String?=null,
                        private val confirmCallback: ((String)->Unit)?=null
                        ) :
        AppCompatDialog(context) ,View.OnClickListener {



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        window?.run {
            requestFeature(Window.FEATURE_NO_TITLE)
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        }


        setContentView(R.layout.dialog_choose_date_img_layout)

        img_top.setImageResource(imgRes)
        tv_title.text = title

        //默认时间

        defDate?.runCatching {
            //转换
            split('-').mapNotNull { it.toIntOrNull() }.let {list ->
                if (list.size == 3){
                    dp_date.updateDate(list[0],list[1]-1,list[2])
                }
            }

        }

        tv_cancel.setOnClickListener(this)
        tv_confirm.setOnClickListener(this)

    }

    override fun onClick(v: View?) {

        when(v?.id){
            R.id.tv_cancel -> {
                //取消

            }
            R.id.tv_confirm -> {
                //确定

                val m = "${dp_date.year}-${(dp_date.month+1).paddingZero()}-${dp_date.dayOfMonth.paddingZero()}"
                confirmCallback?.invoke(m)
            }
        }

        dismiss()
    }

    override fun show() {
        super.show()

        window?.run {

            attributes?.run {
                gravity = Gravity.BOTTOM
                width = WindowManager.LayoutParams.MATCH_PARENT
                height = WindowManager.LayoutParams.WRAP_CONTENT
                attributes = this
            }
            setWindowAnimations(R.style.DialogBottom)
            decorView.setPadding(0, 0, 0, 0)
        }

    }

}